﻿using System;
using System.Drawing;

namespace ArtMath.Core.Data
{
    [Serializable]
    public struct SizeD
    {
        public static readonly SizeD Empty = new SizeD(0, 0);
        public double Width { get; set; }
        public double Height { get; set; }
        public SizeF ToSizeF()
        {
            return new SizeF((float)Width, (float)Height);
        }
        public bool IsMassPoint
        {
            get { return Width == 0 || Height == 0; }
        }
        public SizeD(double width, double height)
        {
            Width = width;
            Height = height;
        }
        public static SizeD FromPointD(PointD pt)
        {
            return new SizeD(pt.X, pt.Y);
        }
        public static SizeD FromSize(Size sz)
        {
            return new SizeD(sz.Width, sz.Height);
        }
        public static SizeD FromSizeF(SizeF sz)
        {
            return new SizeD(sz.Width, sz.Height);
        }
        public static SizeD operator +(SizeD l, SizeD r)
        {
            return new SizeD(l.Width + r.Width, l.Height + r.Height);
        }
        public static SizeD operator -(SizeD l, SizeD r)
        {
            return new SizeD(l.Width - r.Width, l.Height - r.Height);
        }
        public static SizeD operator /(SizeD l, PointD r)
        {
            return new SizeD(l.Width / r.X, l.Height / r.Y);
        }
        public static bool operator ==(SizeD l,SizeD r)
        {
            return l.Width == r.Width && l.Height == r.Height;
        }
        public static bool operator !=(SizeD l, SizeD r)
        {
            return !(l == r);
        }
        public override bool Equals(object obj)
        {
            if (obj is SizeD)
            {
                SizeD sz = (SizeD)obj;
                return this == sz;
            }
            return false;
        }
        public override int GetHashCode()
        {
            long bits = BitConverter.DoubleToInt64Bits(Width) ^ (BitConverter.DoubleToInt64Bits(Height) * 31);
            return (int)bits ^ (int)(bits >> 32);
        }
        public override string ToString()
        {
            return $"{Width}, {Height}";
        }
    }
}
